import { useGlobalStore } from '@/store'

const install = () => {
    /**
     * group By
     * @param {*} group
     */
    // Array.prototype.groupBy = function(group: Function){
    //     return group && typeof group==='function'?Array.prototype.reduce.call(this, function(c, v){
    //         var k = group(v);
    //         if(c.hasOwnProperty(k)){
    //             c[k].push(v);
    //         } else {
    //             c[k] = [v];
    //         }
    //         return c;
    //     },{}):this;
    // }

    uni.$u.$util = {
        /**
         * 根据urlType 跳转
         * @param {String,Number}  urlId
         * @param {String,Number}  urlType
         * @param {String} url 自定义跳转网址
         * @param {String} text 用于搜索页面 显示 关键字
         */
        jumpByUrlType(id: string | number, type: string | number, url: string, text: string) {
            let para = {}
            switch (Number(type)) {
                case 1:
                    console.log('暂无此类型跳转方式')
                    break
                case 2:
                    para = {
                        filter: {
                            groupId: [id]
                        },
                        keyword: text
                    }
                    uni.$u.$MRouter.list.to({ para: JSON.stringify(para) })
                    break
                case 3:
                    para = {
                        filter: {
                            labelIds: [id]
                        },
                        keyword: text
                    }
                    uni.$u.$MRouter.list.to({ para: JSON.stringify(para) })
                    break
                case 4:
                    uni.$u.$MRouter.details.to({ id })
                    break
                case 5:
                    if (!url) break
                    para = JSON.stringify({ url })
                    uni.$u.$MRouter.custom.to({ para })
                    break
                case 6:
                    useGlobalStore().current = id
                    uni.$u.$MRouter.category.to()
                    break
                default:
                    console.log('参数不合法!')
                    break
            }
        },
        /**
         * 去除参数中的非法值  null '' undefined [] {}
         * @param {Object} params 参数对象
         */
        dealNullParams(params: any) {
            let obj: any = {}
            if (params) {
                for (let name in params) {
                    // '' == 0    true  所以为了排除空字符串  使用 params[name] === 0 || params[name] === '0'
                    if (params[name] || params[name] === 0 || params[name] === '0') {
                        if (typeof params[name] == 'object') {
                            // 排除空数组的情况
                            if (Array.isArray(params[name])) {
                                params[name].length > 0 ? (obj[name] = params[name]) : ''
                            } else {
                                Object.keys(params[name]).length > 0 ? (obj[name] = params[name]) : ''
                            }
                        } else {
                            obj[name] = params[name]
                        }
                    }
                }
            }
            return obj
        }
    }
}

export default {
    install
}
